package com.bst.bsj.bops.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.RoleResourceService;
import com.bst.bsj.common.dal.dataobject.RoleResource;
import com.bst.bsj.portal.util.StringUtil;

@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/roleResource")
public class RoleResourceController extends WebBaseController {

  @Autowired
  private RoleResourceService roleResourceService;

  /**
   * 
  * @Title: changeMenu
  * @Description: 添加或修改菜单权限
  * @param req
  * @param resp
  * @throws Exception
  * void
  * @throws 
  * @creator     :yj
  * @create date :2016年3月29日 上午11:25:25
  * @modificator :yj
  * @modify date :2016年3月29日 上午11:25:25
   */
  @RequestMapping(value = "/addMenu")
  @ResponseBody
  public String changeMenu(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    String[] menuList = req.getParameterValues("menuName");
    String roleId = (String) req.getParameter("roleId");
    Long id = null;
    if (StringUtil.isNotBlank(roleId)) {
      id = Long.parseLong(roleId);
    } else {
      return "false";
    }

    //删除原有菜单
    roleResourceService.delete(id);
    RoleResource roleResource = new RoleResource();
    roleResource.setRoleId(id);

    roleResource.setResource("全局父菜单");
    roleResourceService.save(roleResource);
    roleResource.setResource("系统登录");
    roleResourceService.save(roleResource);
    
    
    //保存现有菜单
    for (String menuName : menuList) {
      roleResource.setResource(menuName);
      roleResourceService.save(roleResource);
    }

    return "success";
  }

  /**
  * @Title: showMenu
  * @Description: 回显菜单
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * List<String>
  * @throws 
  * @creator     :yj
  * @create date :2016年5月14日 下午2:40:36
  * @modificator :yj
  * @modify date :2016年5月14日 下午2:40:36
   */
  @RequestMapping(value = "/ showMenu")
  @ResponseBody
  public List<RoleResource>  showMenu(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    String roleId = (String) req.getParameter("roleId");
    Long id = null;
    if (StringUtil.isNotBlank(roleId)) {
      id = Long.parseLong(roleId);
    } 
    List<RoleResource> menuList=roleResourceService.getResourceByRoleId(id);
 
    return menuList;
  }
}